package com.youhome.clients.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.model.Principal;
import com.youhome.service.service.HouseFavouriteService;
import com.youhome.service.service.ListHubService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2018/06/27.
 */
@RestController
@RequestMapping(value = "/favourite")
@Api(tags = "收藏")
public class FavouriteController {
    @Autowired
    private ListHubService listHubService;
    @Autowired
    private HouseFavouriteService houseFavouriteService;

    @ApiOperation(value = "收藏/取消房源", notes = "收藏/取消房源", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "action", value = "save/unsave", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "houseId", required = true, paramType = "path")
    })
    @PostMapping(value = "/{action}/{houseId}")
    public Object saveFavorite(@PathVariable String action, @PathVariable String houseId,@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        houseFavouriteService.saveFavorite(principal,action, houseId);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "收藏列表", notes = "收藏列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "页码", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query")
    })
    @GetMapping(value = "/list")
    public Object list(Integer pageNumber, Integer pageSize,@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new ResultPage<>(ResultCode.SUCCESS, listHubService.selectFavouriteHouseByUserId(principal,pageNumber, pageSize));
    }

    @ApiOperation(value = "收藏房源ID列表", notes = "收藏房源ID列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/houseId")
    public Object houseId(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, houseFavouriteService.selectAllFavouriteHouseId(principal));
    }
}
